function [V,indices,kappas] = predict(X,Y,s,a,h)
%
% function V = predict(X,Y,s,a,h)
%
% hedger predictor function
%
% inputs:  X,Y = current data and values
%          s,a = current state and action
%          h   = LWR bandwidth parameter
%
% outputs: V   = estimated value for state-action
%
% Diego Pontoriero, 2007-4-14

%	default h value

if nargin < 5
	h = 1.0;
end

% concatenate s and a into a row vector
q_bar = [s; a]';

% set some parameters
k_thresh = h*10;
k_min = size(q_bar,1);
dont_know = 0;
indices = [];
kappas = ones(size(X,1),1);

% base case
if size(X,1) == 0
	V = dont_know;
	return;
end
%keyboard
% find set of points K closer to q_bar than k_thresh by euclidean distance
% n.b. for speed, we could use weighted kd-tree, but requires space for storage etc.
% instead, we'll calculate it very simply (our learning space should be rather small)
% indices = find(sqrt(sum((repmat(q_bar,size(X,1),1) - X).^2,2)) < k_thresh);
<<<<<<< .mine
indices = sqrt(sum((q_bar(ones(size(X,1),1),:) - X).^2,2)) < k_thresh;
=======
indices = find(sum((q_bar(ones(size(X,1),1),:) - X).^2,2) < k_thresh.^2);
>>>>>>> .r89
K = X(indices,:);
b = Y(indices);

%
%aa=sum(a.*a,1); bb=sum(b.*b,1); ab=a'*b; 
%d = sqrt(abs(repmat(aa',[1 size(bb,2)]) + repmat(bb,[size(aa,2) 1]) - 2*ab));
%

if size(K,1) < k_min
	V = dont_know;	% need at least as many points as # params to make prediction
else
	% calculate H = independent variable hull (IVH) based on K
	invKK = pinv(K'*K);
	H = K*invKK*K';
	maxHii = max(diag(H));

	if q_bar*invKK*q_bar' <= maxHii	% means that q_bar is inside the hull
		% calculate kernel weight kappa(i) for each k(i) in K
		% kappas = exp(-sum((repmat(q_bar,size(K,1),1) - K).^2,2)/h^2);
		kappas = exp(-sum((q_bar(ones(size(K,1),1),:) - K).^2,2)/h^2);
		W = diag(kappas);
		% do local weighted linear regression on K
		% tobi
        %theta = (K'*W*K)\(K'*W*b);
        A = (K'*W*K); bb = (K'*W*b);
        theta = pinv(A)*bb;
        % check! 
        %A*theta-bb
		% return fitted function f(s,a)
		V = q_bar*theta;
	else	% q_bar outside the hull so we don't make a prediction
		V = dont_know;
	end
end
if any(isnan(V)), keyboard, end
